home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / progtool / c / gcc / crsdoc16.zoo / doc / life.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-27  |  3.0 KB  |  142 lines

  1. /*
  2.  * Copyright 1980 Kenneth C. R. C. Arnold and The Regents of the
  3.  * University of California.  Permission is granted to freely
  4.  * distribute curses and its documentation provided that this
  5.  * notice is left intact.
  6.  */
  7.  
  8. #ifndef lint
  9. static char sccsid[] = "@(#)life.c    6.1 (Berkeley) 4/23/86";
  10. #endif not lint
  11.  
  12. # include    <curses.h>
  13. # include    <signal.h>
  14.  
  15. /*
  16.  *    Run a life game.  This is a demonstration program for
  17.  * the Screen Updating section of the -lcurses cursor package.
  18.  */
  19.  
  20. typedef struct lst_st {            /* linked list element */
  21.     int        y, x;        /* (y, x) position of piece */
  22.     struct lst_st    *next, *last;    /* doubly linked */
  23. } LIST;
  24.  
  25. LIST    *Head;            /* head of linked list */
  26.  
  27. int    die();
  28.  
  29. main(ac, av)
  30. int    ac;
  31. char    *av[];
  32. {
  33.     evalargs(ac, av);        /* evaluate arguments */
  34.  
  35.     initscr();            /* initialize screen package */
  36.     signal(SIGINT, die);        /* set to restore tty stats */
  37.     cbreak();            /* set for char-by-char */
  38.     noecho();            /*    input */
  39.     nonl();                /* for optimization */
  40.  
  41.     getstart();            /* get starting position */
  42.     for (;;) {
  43.         prboard();        /* print out current board */
  44.         update();        /* update board position */
  45.     }
  46. }
  47.  
  48. /*
  49.  * This is the routine which is called when rubout is hit.
  50.  * It resets the tty stats to their original values.  This
  51.  * is the normal way of leaving the program.
  52.  */
  53. die()
  54. {
  55.     signal(SIGINT, SIG_IGN);        /* ignore rubouts */
  56.     mvcur(0, COLS - 1, LINES - 1, 0);    /* go to bottom of screen */
  57.     endwin();                /* set terminal to good state */
  58.     exit(0);
  59. }
  60.  
  61. /*
  62.  * Get the starting position from the user.  They keys u, i, o, j, l,
  63.  * m, ,, and . are used for moving their relative directions from the
  64.  * k key.  Thus, u move diagonally up to the left, , moves directly down,
  65.  * etc.  x places a piece at the current position, " " takes it away.
  66.  * The input can also be from a file.  The list is built after the
  67.  * board setup is ready.
  68.  */
  69. getstart()
  70. {
  71.     reg char    c;
  72.     reg int        x, y;
  73.     auto char    buf[100];
  74.  
  75.     box(stdscr, '|', '_');        /* box in the screen */
  76.     move(1, 1);            /* move to upper left corner */
  77.  
  78.     for (;;) {
  79.         refresh();        /* print current position */
  80.         if ((c = getch()) == 'q')
  81.             break;
  82.         switch (c) {
  83.           case 'u':
  84.           case 'i':
  85.           case 'o':
  86.           case 'j':
  87.           case 'l':
  88.           case 'm':
  89.           case ',':
  90.           case '.':
  91.             adjustyx(c);
  92.             break;
  93.           case 'f':
  94.             mvaddstr(0, 0, "File name: ");
  95.             getstr(buf);
  96.             readfile(buf);
  97.             break;
  98.           case 'x':
  99.             addch('X');
  100.             break;
  101.           case ' ':
  102.             addch(' ');
  103.             break;
  104.         }
  105.     }
  106.  
  107.     if (Head != NULL)            /* start new list */
  108.         dellist(Head);
  109.     Head = malloc(sizeof (LIST)); 
  110.  
  111.     /*
  112.      * loop through the screen looking for 'x's, and add a list
  113.      * element for each one
  114.      */
  115.     for (y = 1; y < LINES - 1; y++)
  116.         for (x = 1; x < COLS - 1; x++) {
  117.             move(y, x);
  118.             if (inch() == 'x')
  119.                 addlist(y, x);
  120.         }
  121. }
  122.  
  123. /*
  124.  * Print out the current board position from the linked list
  125.  */
  126. prboard() {
  127.  
  128.     reg LIST    *hp;
  129.  
  130.     erase();            /* clear out last position */
  131.     box(stdscr, '|', '_');        /* box in the screen */
  132.  
  133.     /*
  134.      * go through the list adding each piece to the newly
  135.      * blank board
  136.      */
  137.     for (hp = Head; hp; hp = hp->next)
  138.         mvaddch(hp->y, hp->x, 'X');
  139.  
  140.     refresh();
  141. }
  142.